<?

function lister($orderby,$ordertype,$limit,$usertype,$type,$group,$uniq_ip){
// echo "TYPE: ".$type."\n\n\n";
$loadtime=date("U");   
 global $session_db_name,$dbh,$sid,$limit;
    
 $last["hour"] = date("Y-m-d H:i:s",mktime(date("H")-1,date("i"),date("s"),date("m"),date("d"),date("Y")));
 $last["24"] = date("Y-m-d H:i:s",mktime(date("H")-24,date("i"),date("s"),date("m"),date("d"),date("Y")));
 $last["week"] = date("Y-m-d H:i:s",mktime(date("H"),date("i"),date("s"),date("m"),date("d")-7,date("Y")));
 $last["30days"] = date("Y-m-d H:i:s",mktime(date("H"),date("i"),date("s"),date("m"),date("d")-30,date("Y")));
 
 $temp="((r.date>'".$last["hour"]."')+(r.date>'".$last["24"]."')+(r.date>'".$last["week"]."')+(r.date>'".$last["30days"]."'))";  
 
        
 
 if(!$orderby){
     $orderby=1;
 }
    
     if($orderby==1) $buff="(count(date)*(date>'".$last["hour"]."')) as buff" ;//1h;
     else if($orderby==2) $buff="(count(r.date)*(r.date>'".$last["24"]."')) as buff"; //24h
     else if($orderby==3) $buff="(count(r.date)*(r.date>'".$last["week"]."')) as buff";//1w
     else if($orderby==4)$buff="(count(r.date)*(r.date>'".$last["30days"]."')) as buff";
     else $buff="r.referer as buff" ;
     
    

if($group){
	if(!$where) $where = " where ";
	else $where .= " and ";
	$where.= " r.referer = rg.referer and rg.groupname='$group' ";
	$second_table = " , referergroups rg ";
}
if($type){
        if(!$where) $where = " where ";
        else $where .= " and ";
        $where.= " r.type = '$type' ";
}


    
    if(!$limit) $limit=50;
 //$mlimit=" limit $limit";
 $mlimit="";

if($uniq_ip) $uniq_all = "distinct r.ip";
else $uniq_all = "r.date";

 $SQL="select r.referer, r.type,count($uniq_all),$temp, $buff  from referers r $second_table  $where group by r.type,r.referer,".$temp." order by buff $ordertype".$mlimit;

//echo $SQL;
    if (!($result = @mysql_db_query($session_db_name, $SQL, $dbh)))
	return "ERROR\n".$SQL."\n";
    //echo $SQL;  
    while ($row = mysql_fetch_row($result)) {
	if(!$row[5]) $row[5]="NULL";
	$lll="<td><a class='links' href='$row[0]' target='_blank'>".substr($row[0],0,50)."</td><td>$row[1]</td>";


	if(!$row[2]) $row[2]=0;
	if($row[3]==4){
	    $refs[$lll]["hour"]+=$row[2];
	    $refs[$lll]["day"]+=$row[2];
	    $refs[$lll]["week"]+=$row[2];
	    $refs[$lll]["month"]+=$row[2];
	    $refs[$lll]["global"]+=$row[2];
	   
	    $total["hour"]+=$row[2];
	    $total["day"]+=$row[2];
	    $total["week"]+=$row[2];
	    $total["month"]+=$row[2];
            $total["global"] += $row[2];
	     
	}
	if($row[3]==3){
	    $refs[$lll]["day"]+=$row[2];
	    $refs[$lll]["week"]+=$row[2];
	    $refs[$lll]["month"]+=$row[2];
	    $refs[$lll]["global"]+=$row[2];
	    
	    $total["day"]+=$row[2];
	    $total["week"]+=$row[2];
	    $total["month"]+=$row[2];
            $total["global"] += $row[2];

	}
	if($row[3]==2){
	    $refs[$lll]["week"]+=$row[2];
	    $refs[$lll]["month"]+=$row[2];
	    $refs[$lll]["global"]+=$row[2];
	    
	    $total["week"]+=$row[2];
	    $total["month"]+=$row[2];
            $total["global"] += $row[2];
	}
       	if($row[3]==1){
	    $refs[$lll]["month"]+=$row[2];
	    $refs[$lll]["global"]+=$row[2];
	    
	    $total["month"]+=$row[2];
            $total["global"] += $row[2];	
	}
	if($row[3]==0){
            $refs[$lll]["global"]+=$row[2];

	    $total["global"] += $row[2];
	}
    }
	
    if (!$total["hour"]) $total["hour"]=0;
    if (!$total["day"]) $total["day"]=0;
    if (!$total["week"]) $total["week"]=0;
    if (!$total["month"]) $total["month"]=0;
    if (!$total["global"]) $total["global"]=0;


    $res.="<tr> <td>&nbsp;</td><td>&nbsp</td><td>&nbsp</td>
      <td bgcolor='#EEEEEE'><b>".$total["hour"]."</b></td>
      <td bgcolor='#EEEEEE'><b>".$total["day"]."</b></td>
      <td bgcolor='#EEEEEE'><b>".$total["week"]."</b></td>
      <td bgcolor='#EEEEEE'><b>".$total["month"]."</b></td>
      <td bgcolor='#EEEEEE'><b>".$total["global"]."</b></td>
      </tr>\n";
      $count=0;	
	if(!is_array($refs)) $refs = array();
	$ccolor[$orderby] = " bgcolor = '#FFFFFF' ";
       while((list($key, $value) = each($refs)) && ($count++<$limit)){
		if($count%2 == 0) $bgcol = " bgcolor = '#EEEEEE' ";
		else $bgcol = " bgcolor = '#DDDDDD' ";

    if (!$refs[$key]["hour"]) $refs[$key]["hour"]=0;
    if (!$refs[$key]["day"]) $refs[$key]["day"]=0;
    if (!$refs[$key]["week"]) $refs[$key]["week"]=0;
    if (!$refs[$key]["month"]) $refs[$key]["month"]=0;
    if (!$refs[$key]["global"]) $refs[$key]["global"]=0;

	    $res.="<tr $color onMouseover=this.style.backgroundColor='#ffbb88' onMouseout=this.style.backgroundColor='' $bgcol >";
	   $res.="<td>$count</td>"; 
	   $res.= $key."</td><td $ccolor[1]>".$refs[$key][hour].
	      "</td><td $ccolor[2]>".$refs[$key][day].
	      "</td><td $ccolor[3]>".$refs[$key][week].
	      "</td><td $ccolor[4]>".$refs[$key][month].
	      "</td><td $ccolor[5]>".$refs[$key]["global"].
	      "</td>"; 
	    $res.="</tr>\n";
	}
	
    $loadtime=date("U") - $loadtime;
    $res .= "<tr><td colspan='20' align='right'>Finished for : $loadtime seconds</td></tr>";
    return $res;
}

?>
